#include "../thread_pool.h"
#include <iostream>
#include <chrono>

// g++ -std=c++11 -pthread thread_pool_example.cpp -o thread_pool_example
int main() {
	ThreadPool pool(4); // 4线程

	// 提交并获取 future
	auto fut1 = pool.enqueue([] { std::cout << "Hello from thread pool!\n"; });
	auto fut2 = pool.enqueue([](int x) { return x * x; }, 6);
	auto fut3 = pool.enqueue([]() {
		std::this_thread::sleep_for(std::chrono::milliseconds(500));
		std::cout << "Task with delay done!\n";
		return 42;
	});

	fut1.get(); // 等待并输出
	std::cout << "fut2 result: " << fut2.get() << "\n";
	std::cout << "fut3 result: " << fut3.get() << "\n";

	// 提交多个任务
	std::vector<std::future<int>> results;
	for (int i = 0; i < 8; ++i) {
		results.push_back(pool.enqueue([i] {
			std::this_thread::sleep_for(std::chrono::milliseconds(100 * i));
			return i * 10;
		}));
	}
	for (auto &fut: results) {
		std::cout << "Result: " << fut.get() << "\n";
	}

	std::cout << "All tasks done!" << std::endl;
	return 0;
}
